package com.noble.meether.chat.ui.agora.beauty;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.noble.library.log.PSP;
import com.noble.meether.R;
import com.noble.glamour.module.logic.application.App;

import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar;

/**
 * Created by chengxiaobo on 2017/7/11.
 */

public class ViewFaceBeautify extends LinearLayout implements View.OnClickListener {
    private final String TAG = "ViewFaceBeautify";
    private BeautityFaceInterface mInterface;

    //    private RecyclerView mEffectRecyclerView;
//    private EffectAndFilterSelectAdapter mEffectRecyclerAdapter;
    private RecyclerView mFilterRecyclerView;
    private EffectAndFilterSelectAdapter mFilterRecyclerAdapter;

    private LinearLayout mBlurLevelSelect;
    private LinearLayout mColorLevelSelect;
    private LinearLayout mFaceShapeSelect;
    private LinearLayout mRedLevelSelect;

    //    private Button mChooseEffectBtn;
    private Button mChooseFilterBtn;
    private Button mChooseBlurLevelBtn;
    private Button mChooseColorLevelBtn;
    private Button mChooseFaceShapeBtn;
    private Button mChooseRedLevelBtn;

    private TextView[] mBlurLevels;
    private int[] BLUR_LEVEL_TV_ID = {R.id.blur_level0, R.id.blur_level1, R.id.blur_level2,
            R.id.blur_level3, R.id.blur_level4, R.id.blur_level5, R.id.blur_level6};

    private TextView mFaceShape0Nvshen;
    private TextView mFaceShape1Wanghong;
    private TextView mFaceShape2Ziran;
    private TextView mFaceShape3Default;

    private LinearLayout mMainBottom;

    private DiscreteSeekBar colorLevelSeekbar; //美白进度条
    private DiscreteSeekBar redLevelShapeLevelSeekbar;//红润进度条
    private DiscreteSeekBar faceShapeLevelSeekbar;//美型程度
    private DiscreteSeekBar enlargeEyeSeekbar;//大眼程度
    private DiscreteSeekBar cheekThinSeekbar;//瘦脸程度

    // 状态保存
    private static final String LEVEL_NATURE = "level_nature";     // 滤镜
    public static final String LEVEL_NATURE_POSI = "level_nature_position";     // 滤镜position
    private static final String LEVEL_BLUR = "level_blur";    // 磨皮
    private static final String LEVEL_COLOR = "level_color";   // 美白
    private static final String LEVEL_RED = "level_red";   // 红润
    private static final String LEVEL_FACE = "level_face";   // 脸型
    private static final String LEVEL_FACE_SHAPE = "level_face_shape";   // 美型
    private static final String LEVEL_EYE = "level_eye";   // 大眼
    private static final String LEVEL_CHEEK_THIN = "level_cheek_thin";   // 瘦脸

    public ViewFaceBeautify(Context context) {
        super(App.context);
        init();
    }

    public ViewFaceBeautify(Context context, @Nullable AttributeSet attrs) {
        super(App.context, attrs);
        init();
    }

    public ViewFaceBeautify(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(App.context, attrs, defStyleAttr);
        init();
    }

    public void setBeautifyInterface(BeautityFaceInterface i_interface) {

        this.mInterface = i_interface;

        initViewState();

    }

    private void init() {
        LayoutInflater.from(App.context).inflate(R.layout.view_setting_face_beautify, this, true);

        mMainBottom = findViewById(R.id.main_bottom);
        LinearLayout llAll = findViewById(R.id.ll_face_beautify_all);
        llAll.setOnClickListener(view -> {

        });
//        mEffectRecyclerView = (RecyclerView) findViewById(R.id.effect_recycle_view);
//        mEffectRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
//        mEffectRecyclerAdapter = new EffectAndFilterSelectAdapter(mEffectRecyclerView, EffectAndFilterSelectAdapter.RECYCLEVIEW_TYPE_EFFECT);
//        mEffectRecyclerAdapter.setOnItemSelectedListener(new EffectAndFilterSelectAdapter.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(int itemPosition) {
//                Log.d(TAG, "effect item selected " + itemPosition);
//                if(mInterface!=null) {
//                    mInterface.onEffectItemSelected(EffectAndFilterSelectAdapter.EFFECT_ITEM_FILE_NAME[itemPosition]);
//                }
//            }
//        });
//        mEffectRecyclerView.setAdapter(mEffectRecyclerAdapter);

        mFilterRecyclerView = findViewById(R.id.filter_recycle_view);
        mFilterRecyclerView.setLayoutManager(new LinearLayoutManager(App.context, LinearLayoutManager.HORIZONTAL, false));
        mFilterRecyclerAdapter = new EffectAndFilterSelectAdapter(mFilterRecyclerView, EffectAndFilterSelectAdapter.RECYCLEVIEW_TYPE_FILTER);
        mFilterRecyclerAdapter.setOnItemSelectedListener(itemPosition -> {
            Log.d(TAG, "filter item selected " + itemPosition);
            if (mInterface != null) {
                String name = EffectAndFilterSelectAdapter.FILTERS_NAME_VALUE[itemPosition];
                mInterface.onFilterSelected(name);
                PSP.getInstance().put(LEVEL_NATURE, name);
                PSP.getInstance().put(LEVEL_NATURE_POSI, itemPosition);
            }
        });
        mFilterRecyclerView.setAdapter(mFilterRecyclerAdapter);

//        mChooseEffectBtn = (Button) findViewById(R.id.btn_choose_effect);
//        mChooseEffectBtn.setOnClickListener(this);
        mChooseFilterBtn = findViewById(R.id.btn_choose_filter);
        mChooseFilterBtn.setOnClickListener(this);
        mChooseBlurLevelBtn = findViewById(R.id.btn_choose_blur_level);
        mChooseBlurLevelBtn.setOnClickListener(this);
        mChooseColorLevelBtn = findViewById(R.id.btn_choose_color_level);
        mChooseColorLevelBtn.setOnClickListener(this);
        mChooseFaceShapeBtn = findViewById(R.id.btn_choose_face_shape);
        mChooseFaceShapeBtn.setOnClickListener(this);
        mChooseRedLevelBtn = findViewById(R.id.btn_choose_red_level);
        mChooseRedLevelBtn.setOnClickListener(this);

        mFaceShape0Nvshen = findViewById(R.id.face_shape_0_nvshen);
        mFaceShape0Nvshen.setOnClickListener(this);
        mFaceShape1Wanghong = findViewById(R.id.face_shape_1_wanghong);
        mFaceShape1Wanghong.setOnClickListener(this);
        mFaceShape3Default = findViewById(R.id.face_shape_3_default);
        mFaceShape3Default.setOnClickListener(this);
        mFaceShape2Ziran = findViewById(R.id.face_shape_2_ziran);
        mFaceShape2Ziran.setOnClickListener(this);

        mBlurLevelSelect = findViewById(R.id.blur_level_select_block);
        mColorLevelSelect = findViewById(R.id.color_level_select_block);
        mFaceShapeSelect = findViewById(R.id.lin_face_shape);
        mRedLevelSelect = findViewById(R.id.red_level_select_block);

        mBlurLevels = new TextView[BLUR_LEVEL_TV_ID.length];
        for (int i = 0; i < BLUR_LEVEL_TV_ID.length; i++) {
            final int level = i;
            mBlurLevels[i] = findViewById(BLUR_LEVEL_TV_ID[i]);
            mBlurLevels[i].setOnClickListener(v -> {
                setBlurLevelTextBackground(mBlurLevels[level]);

                if (mInterface != null) {
                    mInterface.onBlurLevelSelected(level);
                    PSP.getInstance().put(LEVEL_BLUR, level);
                }
            });
        }

        colorLevelSeekbar = findViewById(R.id.color_level_seekbar);
        colorLevelSeekbar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
                if (mInterface != null) {
                    mInterface.onColorLevelSelected(value, 100);
                    PSP.getInstance().put(LEVEL_COLOR, value);
                }
            }

            @Override
            public void onStartTrackingTouch(DiscreteSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

            }
        });

        cheekThinSeekbar = findViewById(R.id.cheekthin_level_seekbar);
        cheekThinSeekbar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {

                if (mInterface != null) {
                    mInterface.onCheekThinSelected(value, 100);
                    PSP.getInstance().put(LEVEL_CHEEK_THIN, value);
                }
            }

            @Override
            public void onStartTrackingTouch(DiscreteSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

            }
        });

        enlargeEyeSeekbar = findViewById(R.id.enlarge_eye_level_seekbar);
        enlargeEyeSeekbar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
                if (mInterface != null) {
                    mInterface.onEnlargeEyeSelected(value, 100);
                    PSP.getInstance().put(LEVEL_EYE, value);
                }
            }

            @Override
            public void onStartTrackingTouch(DiscreteSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

            }
        });

        faceShapeLevelSeekbar = findViewById(R.id.face_shape_seekbar);
        faceShapeLevelSeekbar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
                if (mInterface != null) {
                    mInterface.onFaceShapeLevelSelected(value, 100);
                    PSP.getInstance().put(LEVEL_FACE_SHAPE, value);
                }
            }

            @Override
            public void onStartTrackingTouch(DiscreteSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

            }
        });

        redLevelShapeLevelSeekbar = findViewById(R.id.red_level_seekbar);
        redLevelShapeLevelSeekbar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
                if (mInterface != null) {
                    mInterface.onRedLevelSelected(value, 100);
                    PSP.getInstance().put(LEVEL_RED, value);
                }
            }

            @Override
            public void onStartTrackingTouch(DiscreteSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

            }
        });

        mMainBottom.setOnClickListener(view -> ViewFaceBeautify.this.setVisibility(View.GONE));
    }

    /*
    初始化View的状态
     */
    private void initViewState() {
        String filter = PSP.getInstance().getString(LEVEL_NATURE, "nature"); // 滤镜

        int blur = PSP.getInstance().getInt(LEVEL_BLUR, 6);
        int colorLevel = PSP.getInstance().getInt(LEVEL_COLOR, 20);//3.美白等级
        int redLevel = PSP.getInstance().getInt(LEVEL_RED, 50); //4.红润程度
        int faceShapeLevel = PSP.getInstance().getInt(LEVEL_FACE_SHAPE, 50);//6.美型程度
        int eyeLevel = PSP.getInstance().getInt(LEVEL_EYE, 50);//7.大眼等级
        int thinFaceLevel = PSP.getInstance().getInt(LEVEL_CHEEK_THIN, 50);//8.瘦脸值
        int faceShap = PSP.getInstance().getInt(LEVEL_FACE, 3); // 默认脸型

        //首次初始化默认选中滤镜
        setEffectFilterBeautyChooseBtnTextColor(mChooseFilterBtn);
        setEffectFilterBeautyChooseBlock(mFilterRecyclerView);

//      1. 默认的滤镜，修改 EffectAndFilterSelectAdapter的这个值，默认是0  private final int FILTER_DEFAULT_CLICK_POSITION = 0;
        setBlurLevelTextBackground(mBlurLevels[blur]); //2.磨皮的等级
        colorLevelSeekbar.setProgress(colorLevel); //3.美白等级
        redLevelShapeLevelSeekbar.setProgress(redLevel);//4.红润程度
        faceShapeLevelSeekbar.setProgress(faceShapeLevel);//6.美型程度
        enlargeEyeSeekbar.setProgress(eyeLevel);//7.大眼等级
        cheekThinSeekbar.setProgress(thinFaceLevel);//8.瘦脸值

        switch (faceShap) {
            case 0:
                setFaceShapeBackground(mFaceShape0Nvshen);
                break;
            case 1:
                setFaceShapeBackground(mFaceShape1Wanghong);
                break;
            case 2:
                setFaceShapeBackground(mFaceShape2Ziran);
                break;
            case 3:
                setFaceShapeBackground(mFaceShape3Default);
                break;
        }

        if (mInterface != null)//调用faceunity接口
        {
            mInterface.onFilterSelected(filter);//1.滤镜：nature
            mInterface.onBlurLevelSelected(blur); //2.磨皮等级默认0
            mInterface.onColorLevelSelected(colorLevel, 100); //3.美白等级
            mInterface.onRedLevelSelected(redLevel, 100);//4.红润程度
            mInterface.onFaceShapeSelected(faceShap);//5.脸型是默认
            mInterface.onFaceShapeLevelSelected(faceShapeLevel, 100);//6.美型程度
            mInterface.onEnlargeEyeSelected(eyeLevel, 100);//7.大眼等级
            mInterface.onCheekThinSelected(thinFaceLevel, 100);//8.瘦脸值
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setBlurLevelTextBackground(TextView tv) {
        mBlurLevels[0].setBackground(getResources().getDrawable(R.drawable.zero_blur_level_item_unselected));
        for (int i = 1; i < BLUR_LEVEL_TV_ID.length; i++) {
            mBlurLevels[i].setBackground(getResources().getDrawable(R.drawable.blur_level_item_unselected));
            mBlurLevels[i].setTextColor(Color.WHITE);
        }
        if (tv == mBlurLevels[0]) {
            tv.setBackground(getResources().getDrawable(R.drawable.zero_blur_level_item_selected));
        } else {
            tv.setBackground(getResources().getDrawable(R.drawable.blur_level_item_selected));
            tv.setTextColor(Color.BLACK);
        }
    }

    private void setFaceShapeBackground(TextView tv) {
//        mFaceShape0Nvshen.setBackgroundColor(ContextCompat.getColor(mContext, R.color.unselect_gray));
//        mFaceShape1Wanghong.setBackgroundColor(ContextCompat.getColor(mContext, R.color.unselect_gray));
//        mFaceShape2Ziran.setBackgroundColor(ContextCompat.getColor(mContext, R.color.unselect_gray));
//        mFaceShape3Default.setBackgroundColor(ContextCompat.getColor(mContext, R.color.unselect_gray));
//        tv.setBackgroundColor(ContextCompat.getColor(mContext,R.color.text_theme_blue));
        mFaceShape0Nvshen.setSelected(false);
        mFaceShape0Nvshen.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mFaceShape1Wanghong.setSelected(false);
        mFaceShape1Wanghong.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mFaceShape2Ziran.setSelected(false);
        mFaceShape2Ziran.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mFaceShape3Default.setSelected(false);
        mFaceShape3Default.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        tv.setSelected(true);
        tv.setTextColor(Color.BLACK);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn_choose_effect:
//                setEffectFilterBeautyChooseBtnTextColor(mChooseEffectBtn);
//                setEffectFilterBeautyChooseBlock(mEffectRecyclerView);
//                break;
            case R.id.btn_choose_filter:
                setEffectFilterBeautyChooseBtnTextColor(mChooseFilterBtn);
                setEffectFilterBeautyChooseBlock(mFilterRecyclerView);
                break;
            case R.id.btn_choose_blur_level:
                setEffectFilterBeautyChooseBtnTextColor(mChooseBlurLevelBtn);
                setEffectFilterBeautyChooseBlock(mBlurLevelSelect);
                break;
            case R.id.btn_choose_color_level:
                setEffectFilterBeautyChooseBtnTextColor(mChooseColorLevelBtn);
                setEffectFilterBeautyChooseBlock(mColorLevelSelect);
                break;
            case R.id.btn_choose_face_shape:
                setEffectFilterBeautyChooseBtnTextColor(mChooseFaceShapeBtn);
                setEffectFilterBeautyChooseBlock(mFaceShapeSelect);
                break;
            case R.id.btn_choose_red_level:
                setEffectFilterBeautyChooseBtnTextColor(mChooseRedLevelBtn);
                setEffectFilterBeautyChooseBlock(mRedLevelSelect);
                break;
            case R.id.face_shape_0_nvshen:
                setFaceShapeBackground(mFaceShape0Nvshen);
                onFaceShapeSelected(0);
                break;
            case R.id.face_shape_1_wanghong:
                setFaceShapeBackground(mFaceShape1Wanghong);
                onFaceShapeSelected(1);
                break;
            case R.id.face_shape_2_ziran:

                setFaceShapeBackground(mFaceShape2Ziran);
                onFaceShapeSelected(2);
                break;
            case R.id.face_shape_3_default:
                setFaceShapeBackground(mFaceShape3Default);
                onFaceShapeSelected(3);
                break;
        }
    }

    private void onFaceShapeSelected(int type) {
        if (mInterface != null) {
            mInterface.onFaceShapeSelected(type);
            PSP.getInstance().put(LEVEL_FACE, type);
        }
    }

    private void setEffectFilterBeautyChooseBtnTextColor(Button selectedBtn) {
//        mChooseEffectBtn.setTextColor(getResources().getColor(R.color.colorWhite));
        mChooseColorLevelBtn.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mChooseBlurLevelBtn.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mChooseFilterBtn.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mChooseFaceShapeBtn.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        mChooseRedLevelBtn.setTextColor(getResources().getColor(R.color.color_C7C7C7));
        selectedBtn.setTextColor(getResources().getColor(R.color.text_theme_blue));

    }

    private void setEffectFilterBeautyChooseBlock(View v) {
//        mEffectRecyclerView.setVisibility(View.INVISIBLE);
        mFilterRecyclerView.setVisibility(View.GONE);
        mFaceShapeSelect.setVisibility(View.GONE);
        mBlurLevelSelect.setVisibility(View.GONE);
        mColorLevelSelect.setVisibility(View.GONE);
        mRedLevelSelect.setVisibility(View.GONE);
        v.setVisibility(View.VISIBLE);
    }

    public interface BeautityFaceInterface {
        /**
         * 道具贴纸选择
         *
         * @param effectItemName 道具贴纸文件名
         */
        void onEffectItemSelected(String effectItemName);

        /**
         * 滤镜选择
         *
         * @param filterName 滤镜名称
         */
        void onFilterSelected(String filterName);

        /**
         * 磨皮选择
         *
         * @param level 磨皮level
         */
        void onBlurLevelSelected(int level);

        /**
         * 美白选择
         *
         * @param progress 美白滑动条进度
         * @param max      美白滑动条最大值
         */
        void onColorLevelSelected(int progress, int max);

        /**
         * 瘦脸选择
         *
         * @param progress 瘦脸滑动进度
         * @param max      瘦脸滑动条最大值
         */
        void onCheekThinSelected(int progress, int max);

        /**
         * 大眼选择
         *
         * @param progress 大眼滑动进度
         * @param max      大眼滑动条最大值
         */
        void onEnlargeEyeSelected(int progress, int max);

        /**
         * 脸型选择
         */
        void onFaceShapeSelected(int faceShape);

        /**
         * 美型程度选择
         */
        void onFaceShapeLevelSelected(int progress, int max);

        /**
         * 红润程度选择
         */
        void onRedLevelSelected(int progress, int max);
    }

    // 滤镜的recyclervire右滑受限
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN)
            getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }
}
